<template>
  <el-drawer v-model="showDrawer" :direction="direction" :width="800" :before-close="handleClose">
    <template #header>
      <h4 style="font-size: 16px; font-weight: 600">详情</h4>
    </template>
    <template #default>
      <div class="detail-container">
        <div class="detail-title">
          {{ newsForm.title }}
        </div>
        <div class="detail-meta">
          <span class="source">{{ newsForm.source || '未知来源' }}</span>
          <span class="time">{{ newsTime || '未知时间' }}</span>
          <a :href="newsForm.url || '#'" class="original-link" target="_blank">原文链接</a>
        </div>

        <div class="detail-summary">
          <p :class="{ 'line-clamp-1' : !expand}">{{ newsForm.summary }}</p>
          <span class="expand-btn" @click="conceal"> {{ expand ? '收起' : '展开' }}</span>
        </div>

        <div class="detail-content">
          <p v-if="!contentLoaded">加载中...</p>
          <template v-else>
            <p>
              ### {{ newsForm.title }}
              这是新闻的详细内容，包含了丰富的信息和分析。本文档详细介绍了相关事件的背景、发展过程和未来展望。
            </p>

            <p>
              ### 主要内容 - 详细介绍了相关领域的最新进展 - 分析了当前面临的挑战和机遇 -
              探讨了发展趋势对未来的影响
            </p>

            <p>### 专家观点 行业专家表示，这一发展将带来新的变革，促进相关产业的升级和创新。</p>
          </template>
        </div>
      </div>
    </template>
    <template #footer>
      <div style="display: flex; justify-content: flex-end; gap: 10px">
        <el-button @click="handleCancel">关闭</el-button>
      </div>
    </template>
  </el-drawer>
</template>

<script lang="ts" setup>
import { ref, watch, onMounted } from 'vue'
import type { DrawerProps } from 'element-plus'

const props = defineProps<{
  newsData?: {
    title?: string
    source?: string
    publishTime?: string
    summary?: string
    url?: string
    id?: string
  }
}>()

const emit = defineEmits(['close'])

const showDrawer = ref(true)
const direction = ref<DrawerProps['direction']>('rtl')
const contentLoaded = ref(false)

const newsForm = ref({
  title: '',
  source: '',
  url: '',
  summary: '',
})


const newsTime = ref('')
const expand = ref(false)
const conceal = () => {
  expand.value = !expand.value
}

watch(
  () => props.newsData,
  (newData) => {
    if (newData) {
      newsForm.value = {
        title: newData.title || '',
        source: newData.source || '',
        url: newData.url || '',
        summary: newData.summary || '',
      }
      newsTime.value = newData.publishTime || ''
      contentLoaded.value = false
      setTimeout(() => {
        contentLoaded.value = true
      }, 300)
    }
  },
  { immediate: true },
)

const handleClose = (done: () => void) => {
  done()
  emit('close')
}

const handleCancel = () => {
  showDrawer.value = false
  emit('close')
}

onMounted(() => {
  showDrawer.value = true
})
</script>

<style scoped>
.detail-container {
  padding: 0 20px;
  height: calc(100% - 40px);
  overflow-y: auto;
}

.detail-title {
  font-size: 18px;
  font-weight: 600;
  color: #333;
  line-height: 1.5;
  margin-bottom: 15px;
  padding-bottom: 10px;
  border-bottom: 1px solid #eee;
}

.detail-meta {
  display: flex;
  align-items: center;
  color: #999;
  font-size: 14px;
  margin-bottom: 20px;
}

.source {
  margin-right: 15px;
}

.time {
  margin-right: 15px;
}

.original-link {
  color: #1890ff;
  text-decoration: underline;
  cursor: pointer;
}

.detail-summary {
  background-color: #f5f5f5;
  padding: 15px;
  border-radius: 4px;
  margin-bottom: 25px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  min-height: 20px;
}

.detail-summary p :not(.line-clamp-1) {
  margin: 0;
  font-size: 14px;
  color: #666;
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.expand-btn {
  color: #1890ff;
  font-size: 14px;
  cursor: pointer;
  margin-left: 10px;
  white-space: nowrap;
}

.detail-content {
  font-size: 14px;
  color: #333;
  line-height: 1.8;
}

.detail-content p {
  margin: 0 0 20px 0;
}

.detail-content p:before {
  content: '';
  display: block;
  margin-bottom: 8px;
}

.detail-content p:contains('###') {
  font-weight: 600;
  color: #333;
  margin-top: 25px;
}

.detail-content p:contains('-') {
  padding-left: 10px;
}
</style>
